package com.example.appjam;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class Database extends SQLiteOpenHelper{
	private static String DB_PATH = "/data/data/com.example.appjam/databases/";
	private static String DB_NAME = "small_library.sqlite";
	private SQLiteDatabase myDatabase;
	private final Context myContext;
	
	public Database(Context context) {
		super(context, DB_NAME, null, 1);
		// TODO Auto-generated constructor stub
		this.myContext = context;
	}

	public void createDataBase() throws IOException{
		SQLiteDatabase checkDB = null;
		try{
			String myPath = DB_PATH + DB_NAME;
			checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
		}catch(SQLiteException e){
		}
		
		if(checkDB != null){
			checkDB.close();
		}
		
		boolean dbExist = checkDB != null? true : false;
		if(dbExist){
		}
		else{
			this.getReadableDatabase();
			try{
				InputStream myInput = myContext.getAssets().open(DB_NAME);
				String outFileName = DB_PATH + DB_NAME;
				OutputStream myOutput = new FileOutputStream(outFileName);
				byte[] buffer = new byte[1024];
				int length;
				while((length = myInput.read(buffer)) > 0){
					myOutput.write(buffer, 0, length);
				}
				myOutput.flush();
				myOutput.close();
				myInput.close();
			}catch(IOException e){
				throw new Error("Error copying database");
			}
		}
	}
	
	public void openDataBase(){
		String myPath = DB_PATH + DB_NAME;
		myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
	}
	
	public synchronized void close(){
		if(myDatabase != null)
			myDatabase.close();
		super.close();
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}

	public Cursor getAllLibraryList(){
		Cursor c = myDatabase.rawQuery("SELECT * FROM library_list;", null);
		
		if (c != null && c.getCount() != 0)
			c.moveToFirst();

		return c;
	}
	
	public Cursor getAllBookmarkList(){
		Cursor c = myDatabase.rawQuery("SELECT * FROM bookmark_list;", null);
		
		if (c != null && c.getCount() != 0)
			c.moveToFirst();

		return c;
	}
	
	public Cursor getLibraryInfo(String library_name){
		Cursor c = myDatabase.rawQuery("SELECT CTTPC_CN, NEADRES_NM, WEEKDAY, WEEKEND, CLOSEDDAY " +
				"FROM library_list WHERE FCLTY_NM='"+library_name+"';", null);
		
		if (c != null && c.getCount() != 0)
			c.moveToFirst();

		return c;
	}
	
	public Cursor getLibraryList(String area_name, String dong_name){
		Cursor c = myDatabase.rawQuery("SELECT FCLTY_NM " +
				"FROM library_list WHERE GU_NM='"+area_name+"' AND HNR_NAM='"+dong_name+"';", null);
		
		if (c != null && c.getCount() != 0)
			c.moveToFirst();

		return c;
	}
	
	public Cursor isThereBookmarkData(String library_name){
		Cursor c = myDatabase.rawQuery("SELECT * FROM bookmark_list WHERE library_name='"+library_name+"';", null);
		
		if (c != null && c.getCount() != 0)
			c.moveToFirst();

		return c;
	}
	
	public void insertBookmark(int id, String library_name, String area_name, String dong_name, String phone) {
		myDatabase.execSQL("INSERT INTO bookmark_list VALUES ("+id+", '"+library_name+"', '"+area_name+"', '"
																+dong_name+"', '"+phone+"');");
	}
	
	public void deleteBookmarkData(String library_name) {
		myDatabase.execSQL("DELETE FROM bookmark_list WHERE library_name='"+library_name+"';");
	}
	
	public Cursor getBookmarkInsertData(String library_name){
		Cursor c = myDatabase.rawQuery("SELECT OBJECTID, GU_NM, HNR_NAM, CTTPC_CN " +
				"FROM library_list WHERE FCLTY_NM='"+library_name+"';", null);
		
		if (c != null && c.getCount() != 0)
			c.moveToFirst();

		return c;
	}
	
	public Cursor getLibraryPlace(String library_name, String address){
		Cursor c = myDatabase.rawQuery("SELECT LNG, LAT " +
				"FROM library_list WHERE FCLTY_NM='"+library_name+"' AND NEADRES_NM='"+address+"';", null);
		
		if (c != null && c.getCount() != 0)
			c.moveToFirst();

		return c;
	}
	
	public Cursor getDongList(String gu_name){
		Cursor c = myDatabase.rawQuery("SELECT DISTINCT HNR_NAM " +
				"FROM library_list WHERE GU_NM='"+gu_name+"';", null);
		
		if (c != null && c.getCount() != 0)
			c.moveToFirst();

		return c;
	}
}
